﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.Breed_养殖_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Breed_养殖_
{
    public class UpdateFirmHandler : IRequestHandler<UpdateFirmCommand, ApiResult<int>>
    {
        private readonly IBase<FirmModel> firmRepo;
        private readonly IMapper mapper;

        public UpdateFirmHandler(IBase<FirmModel> firmRepo,IMapper mapper)
        {
            this.firmRepo = firmRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(UpdateFirmCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var firm = firmRepo.GetValue(request.FirmId).Result;
             firm = mapper.Map(request,firm);
            var r=firmRepo.Update(firm).Result;
            if(r>0)
            {
                result.Data = r;
                result.Code = ApiEnum.OK;
                result.Msg = "养殖企业信息修改成功";
            }
            else
            {
                result.Data = r;
                result.Code = ApiEnum.Fail;
                result.Msg = "养殖企业信息修改失败";
            }
            return Task.FromResult(result);
        }
    }
}
